#!/bin/bash

# 生成自签名SSL证书的脚本
# 用于测试HTTPS功能

# 检查是否已安装openssl
if ! command -v openssl &> /dev/null
then
    echo "openssl 未安装，请先安装 openssl"
    exit 1
fi

# 生成私钥
echo "生成私钥文件 key.pem..."
openssl genrsa -out key.pem 2048

# 生成证书签名请求
echo "生成证书签名请求 cert.csr..."
openssl req -new -key key.pem -out cert.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=MyOrg/OU=MyUnit/CN=localhost"

# 生成自签名证书
echo "生成自签名证书 cert.pem..."
openssl x509 -req -days 365 -in cert.csr -signkey key.pem -out cert.pem

# 清理中间文件
rm cert.csr

echo "证书生成完成！"
echo "key.pem - 私钥文件"
echo "cert.pem - 证书文件"
echo ""
echo "现在你可以运行HTTPS服务器了："
echo "go run main.go"